import { defineStore } from 'pinia'
import useRouterJump from '@/hooks/useRouterJump'
import {
  addUserApi,
  checkMsApi,
  forgetPasswordApi,
  getVerificationCodePhotoApi,
  loginApi,
  registerApi,
  smsApi,
  smsOneApi
} from '@/service/api'
import type { IState } from './type'
import type {
  addUser,
  checkMs,
  forgetPassword,
  login,
  register,
  sms
} from '@/service/api/login/type'
import { iscode } from '@/utils'
import useProfileStore from '../profile/profile'

const useLoginStore = defineStore('login', {
  state: (): IState => ({
    token: '',
    currentCardType: 'login',
    verificationCodePhoto: '',
    secret: ''
  }),
  actions: {
    // 获取图形验证码
    async getVerificationCodePhotoAction() {
      const res = await getVerificationCodePhotoApi()
      const blob = new Blob([res])
      const url = URL.createObjectURL(blob)
      this.verificationCodePhoto = url
    },
    // 获取短信验证码（注册）
    async smsAction(parms: sms.IParms, successCb?: (cbRes: sms.IRes) => void) {
      const res = await smsApi(parms)
      if (iscode(res, true)) {
        successCb && successCb(res)
      }
    },
    // 获取短信验证码（忘记免密码）
    async smsOneAction(parms: sms.IParms, successCb?: (cbRes: sms.IRes) => void) {
      const res = await smsOneApi(parms)
      if (iscode(res, true)) {
        successCb && successCb(res)
      }
    },
    // 登录（验证码方式）
    async addUserAction(parms: addUser.IParms, successCb?: (cbRes: addUser.IRes) => void) {
      const res = await addUserApi(parms)
      if (iscode(res, true)) {
        const { secret, token } = res.result
        secret && (this.secret = secret)
        token && (this.token = token)

        // 恢复登录卡片type
        this.currentCardType = 'login'

        successCb && successCb(res)
      }
    },
    // 登录（密码方式）
    async loginAction(parms: login.IParms, successCb?: (cbRes: login.IRes) => void) {
      const res = await loginApi(parms)
      if (iscode(res, true)) {
        const { token } = res.result
        token && (this.token = token)

        // 恢复登录卡片type
        this.currentCardType = 'login'

        // 获取用户信息
        const profileStore = useProfileStore()
        profileStore.selectInfoAction({})

        successCb && successCb(res)
      }
    },
    // 注册新用户
    async registerAction(parms: register.IParms, successCb?: (cbRes: register.IRes) => void) {
      const res = await registerApi(parms)
      if (iscode(res, true)) {
        this.token = res.result.token

        // 恢复登录卡片type
        this.currentCardType = 'login'

        // 获取用户信息
        const profileStore = useProfileStore()
        profileStore.selectInfoAction({})

        successCb && successCb(res)
      }
    },
    // 验证忘记密码
    async checkMsAction(parms: checkMs.IParms, successCb?: (cbRes: checkMs.IRes) => void) {
      const res = await checkMsApi(parms)
      if (iscode(res, true)) {
        const { secret } = res.result
        secret && (this.secret = secret)
        successCb && successCb(res)
      }
    },
    // 忘记密码
    async forgetPasswordAction(
      parms: forgetPassword.IParms,
      successCb?: (res: forgetPassword.IRes) => void
    ) {
      const res = await forgetPasswordApi(parms)
      if (iscode(res, true)) {
        this.currentCardType = 'login'
        successCb && successCb(res)
      }
    },
    // 退出登录
    logoutAction() {
      useRouterJump('/login')
      this.$reset()
    }
  },
  persist: {
    storage: localStorage,
    paths: ['currentCardType', 'userInfo', 'token', 'secret']
  }
})

export default useLoginStore
